
var uploadFileEvent = function (options, success) {
    var fileElenemt = null;
    var html = '';
    html += '   <form style="display:none;" action="' + options.url + '" method="post" enctype="multipart/form-data">';
    html += '       <input type="file" class="fileElement" name="' + options.fileKey + '"/>';
    html += '       <input type="submit" style="display: none;"/>';
    html += '   </form>';
    var form = $(html);
    form.ajaxForm({
        success: (result) => {
            $('body').loading();
            if (result.code == 200) {
                success(result.data, fileElenemt);
            } else {
                layer.open({
                    title: '警告',
                    content: result.msg
                });
            };
        },
        error: function (error, b, c) {
            $('body').loading();
            layer.open({
                title: '警告',
                content: error.msg
            });
        }
    });
    form.find('.fileElement').change(function (e) {
        var file = form.find('.fileElement')[0].files[0];
        fileElenemt = file;
        form.submit();
        $('body').loading();
    });
    form.find('.fileElement').click();
};

var uploadFileItem = function (data, parent, readonly) {
    this.data = data;
    this.html = $('<div class="form-uploadFile-item"><i class="fa fa-link" aria-hidden="true"></i><span></span></div>');
    this.html.find('span').text(data.realName);
    this.deleteButton = $('<div class="fa fa-trash-o"></div>').appendTo(this.html);
    this.html.append(this.deleteButton);
    this.deleteButton.click(() => {
        parent.deleteChild(this);
    });
    this.readonly(readonly);
};
uploadFileItem.prototype.readonly = function (data) {
    if (!data) {
        this.deleteButton.show();
    } else {
        this.deleteButton.hide();
    };
};

uploadFileItem.prototype.deleteItem = function () {
    this.html.remove();
};

var uploadFileElement = function (options) {
    this.options = $.extend({}, {
        uploadOption: {
            url: '',
            fileKey: '',
        },
        readonly: false,
        deleteEvent: function () { },
        uploadButtonText: '上传附件',
    }, options);
    this.html = $('<div class="form-uploadFile"></div>');
    this.uploadButton = $('<div class="form-uploadFile-button">' + this.options.uploadButtonText + '</div>').appendTo(this.html);
    this.body = $('<div class="form-uploadFile-content"></div>').appendTo(this.html);

    var _this = this;

    this.children = [];
    this.uploadButton.click(function () {
        uploadFileEvent(_this.options.uploadOption, function (res, file) {
            var data = {
                sid: res,
                realName: file.name,
                type: 'upload',
            };
            _this.addChild(data);
        });
    });
    this.readonly(this.options.readonly);
};
uploadFileElement.prototype.readonly = function (data) {
    if (!data) {
        this.uploadButton.show();
    } else {
        this.uploadButton.hide();
    };
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        element.readonly(data);
    };
};

uploadFileElement.prototype.deleteChild = function (child) {
    layer.confirm('是否删除文件？', (index) => {
        layer.close(index);
        this.options.deleteEvent(child.data, () => {
            for (let i = 0; i < this.children.length; i++) {
                const element = this.children[i];
                if (element == child) {
                    this.children.splice(i, 1);
                    child.deleteItem();
                };
            }
        });
    });
};

uploadFileElement.prototype.addChild = function (data) {
    var child = new uploadFileItem(data, this, this.options.readonly);
    this.children.push(child);
    this.body.append(child.html);
};

uploadFileElement.prototype.get = function (callBack, ignore) {
    var _data = [];
    for (let i = 0; i < this.children.length; i++) {
        const child = this.children[i];
        if (child.data.type == 'upload') {
            _data.push(child.data.sid);
        };
    };
    callBack(false, _data);
    return this;
};


uploadFileElement.prototype.set = function (data) {
    if (!data) {
        return
    };
    for (let i = 0; i < data.length; i++) {
        const item = data[i];
        var _data = {
            sid: item.sid,
            realName: item.realName,
            type: 'set_',
        };
        this.addChild(_data);
    };
};




export default uploadFileElement;

